package com.zkdcloud.controller;

import com.xdja.interceptor.annotation.BeforeProcess;
import com.xdja.interceptor.annotation.Validate;
import com.xdja.interceptor.bean.ResultBean;
import com.xdja.interceptor.exception.InvokeException;
import com.xdja.interceptor.util.ValidateUtils;
import com.zkdcloud.aspectInterceptor.advice.MobileTokenAdvice;
import com.zkdcloud.aspectInterceptor.advice.PageAdvice;
import com.zkdcloud.aspectInterceptor.advice.TokenAdvice;
import com.zkdcloud.aspectInterceptor.validatie.CategoryControllerValidate;
import com.zkdcloud.domain.Admin;
import com.zkdcloud.domain.Category;
import com.zkdcloud.service.CategoryService;
import org.jfaster.mango.plugin.page.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import static com.zkdcloud.constant.Constants.HTTP_CONSTANTS.HTTP_SUCCESS;
import static com.zkdcloud.constant.Constants.HTTP_CONSTANTS.OPERATOR_SUCCESS;
import static com.zkdcloud.constant.Constants.HTTP_CONSTANTS.REQUEST_ADMIN;

/**
 * 后台分类管理
 *
 * @author zk
 * @since 2018/4/18
 */
@RestController
@RequestMapping(value = "/mg/category")
public class CategoryController {
    /**
     * static logger
     */
    private static Logger logger = LoggerFactory.getLogger(CategoryController.class);

    @Autowired
    private CategoryService categoryService;
    /**
     * 分页获取所有分页
     *
     * @param page 分页数据
     * @return resultBean
     */
    @BeforeProcess(advice = {PageAdvice.class, TokenAdvice.class})
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public ResultBean getAll(Page page, Map<String,Object> map) {
        Admin admin = (Admin) map.get(REQUEST_ADMIN);
        List<Category> categoryList = categoryService.getAllByBid(admin.getBusinessId());
        return ResultBean.successResult(HTTP_SUCCESS,categoryList);
    }

    /**
     * 移除指定分类(包括其下的美食)
     *
     * @param id id
     * @return resultBean
     */
    @RequestMapping(value = "/remove/{id}", method = RequestMethod.POST)
    public ResultBean remove(@PathVariable("id") String id) {
        categoryService.remove(id);
        return ResultBean.successResult(OPERATOR_SUCCESS);
    }

    /**
     * 添加分类名称
     *
     * @param category 分类
     * @return resultBean
     */
    @BeforeProcess(advice = TokenAdvice.class,
            validate = @Validate(value = CategoryControllerValidate.class, method = "add"))
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResultBean add(Category category,Map<String,Object> map) {
        categoryService.addCategory(category);
        return ResultBean.successResult(OPERATOR_SUCCESS);
    }

    /**
     * 更新指定分类
     *
     * @param category 分类
     * @return rb
     * @throws InvokeException iv
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public ResultBean update(Category category) throws InvokeException {
        ValidateUtils.assertCondition(!StringUtils.isEmpty(category.getId()),"id is not null");
        categoryService.update(category);
        return ResultBean.successResult(OPERATOR_SUCCESS);
    }
}
